package com.ruibang.glass.portal.controller;

import com.alibaba.fastjson.JSON;
import com.ruibang.glass.common.domain.DictInfo;
import com.ruibang.glass.portal.domain.*;
import com.ruibang.glass.portal.entity.UserEntity;
import com.ruibang.glass.portal.service.UserService;
import com.teaming.cloud.framework2.common.pojo.PageRequest;
import com.teaming.cloud.framework2.common.pojo.PageResult;
import com.teaming.cloud.framework2.common.model.ResultBody;
import com.teaming.cloud.framework2.screw.component.ScrewTemplate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api/user")
@Slf4j
@Api(tags = "用户管理")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private ScrewTemplate screwTemplate;

    @ApiOperation(value = "获取用户列表", response = ResultBody.class)
    @PostMapping(value = "/getUserList")
    public ResultBody<PageResult<List<UserEntity>>> getUserList(@RequestBody PageRequest<UserPageParams> pageRequest) {
        log.info("开始获取用户列表：{}", JSON.toJSONString(pageRequest));
        return userService.getUserList(pageRequest);
    }

    @ApiOperation(value = "根据参数获取用户列表", response = ResultBody.class)
    @PostMapping(value = "/getUserByParams")
    public ResultBody<List<UserEntity>> getUserByParams(@RequestBody UserPageParams params) {
        return userService.getUserByParams(params);
    }

    @ApiOperation(value = "保存用户", response = ResultBody.class)
    @PostMapping(value = "/saveUser")
    public ResultBody<Void> saveUser(@RequestBody @Valid SaveUserReq req) {
        log.info("开始保存用户：{}", JSON.toJSONString(req));
        return userService.saveUser(req);
    }

    @ApiOperation(value = "注册", response = ResultBody.class)
    @PostMapping(value = "/register")
    public ResultBody<Void> register(@RequestBody RegisterParams params) {
        return userService.register(params);
    }

    @ApiOperation(value = "更新用户信息", response = ResultBody.class)
    @PostMapping(value = "/updateUser")
    public ResultBody<Void> updateUser(@RequestBody @Valid UserEntity userInfo) {
        return userService.updateUserInfoByUserId(userInfo);
    }

    @ApiOperation(value = "重置密码", response = ResultBody.class)
    @PostMapping(value = "/updatePassword")
    public ResultBody<Void> updatePassword(@RequestBody @Valid UserPass user) {
        return userService.updateUserPassword(user);
    }

    @ApiOperation(value = "修改密码", response = ResultBody.class)
    @PostMapping(value = "/modifyPassword")
    public ResultBody<Void> modifyPassword(@RequestBody @Valid UserPass user) {
        return userService.modifyPassword(user);
    }

    @ApiOperation(value = "删除用户", response = ResultBody.class)
    @PostMapping(value = "/deleteUserById/{userId}")
    public ResultBody<Void> deleteUserById(@PathVariable("userId") String userId) {
        return userService.deleteByUserId(userId);
    }

    @ApiOperation(value = "获取用户信息", response = ResultBody.class)
    @GetMapping(value = "/getUserInfoById/{userId}")
    public ResultBody<UserEntity> getUser(@PathVariable("userId") String userId) {
        log.info("根据用户id获取用户信息：{}", userId);
        return userService.getUser(userId);
    }

    @ApiOperation(value = "退出登录", response = ResultBody.class)
    @GetMapping(value = "/logout/{tokenInfo}")
    public ResultBody<Void> logout(@PathVariable("tokenInfo") String tokenInfo) {
        log.info("开始退出登录：{}", tokenInfo);
        return userService.logout(tokenInfo);
    }

    @ApiOperation(value = "通过token获取信息", response = ResultBody.class)
    @GetMapping(value = "/getTokenInfo/{tokenInfo}")
    public ResultBody<BindResponse> getTokenInfo(@PathVariable("tokenInfo") String tokenInfo) {
        log.info("开始通过token获取信息:{}", tokenInfo);
        return userService.getTokenInfo(tokenInfo);
    }

    @ApiOperation(value = "图形验证码", response = ResultBody.class)
    @GetMapping(value = "/generGraphCode")
    public void generGraphCode(HttpServletResponse response) {
        userService.generGraphCode(response);
    }

    @ApiOperation(value = "人脸登录", response = ResultBody.class)
    @PostMapping(value = "/faceLogin")
    public ResultBody<BindResponse> faceLogin(@RequestBody LoginParams params, HttpServletRequest request) {
        return userService.faceLogin(params, request);
    }

    @ApiOperation(value = "登录", response = ResultBody.class)
    @PostMapping(value = "/login")
    public ResultBody<BindResponse> login(@RequestBody LoginParams params, HttpServletRequest request) {
        return userService.login(params, request);
    }


    @ApiOperation(value = "根据LoginName获取登录信息", response = ResultBody.class)
    @GetMapping(value = "/getLoginByLoginName/{loginName}")
    public ResultBody<BindResponse> getLoginByLoginName(@PathVariable("loginName") String loginName,HttpServletRequest request) {
        return userService.getLoginByLoginName(loginName,request);
    }

    @ApiOperation(value = "短信登录", response = ResultBody.class)
    @PostMapping(value = "/note/login")
    public ResultBody<BindResponse> noteLogin(@RequestBody NoteLoginReq noteLoginReq, HttpServletRequest request) {
        return userService.noteLogin(noteLoginReq, request);
    }

    @ApiOperation(value = "获取短信验证码", response = ResultBody.class)
    @GetMapping(value = "/note/getNoteCode/{phoneNum}")
    public ResultBody getNoteCode(@PathVariable("phoneNum") String phoneNum) {
        return userService.getNoteCode(phoneNum);
    }

    @ApiOperation(value = "获取所有用户Id", response = ResultBody.class)
    @GetMapping(value = "/getUserIds")
    public ResultBody<List<String>> getUserIds() {
        return ResultBody.success(userService.getUserIds());
    }

    @ApiOperation(value = "设置客户端Id", response = ResultBody.class)
    @GetMapping(value = "/setClientId/{cid}")
    public ResultBody setClientId(@PathVariable("cid") String cid) {
        userService.setClientId(cid);
        return ResultBody.success();
    }

    @ApiOperation(value = "校验手机号是否存在", response = ResultBody.class)
    @GetMapping(value = "/note/checkPhoneNum/{phoneNum}")
    public ResultBody checkPhoneNum(@PathVariable("phoneNum") String phoneNum) {
        return userService.checkPhoneNum(phoneNum);
    }

    @ApiOperation(value = "根据用户Id查名称", response = ResultBody.class)
    @PostMapping(value = "/getUserNamesByUserIds")
    public ResultBody<List<DictInfo>> getUserNamesByUserIds(@RequestBody List<String> userIds) {
        return ResultBody.success(userService.getUserNamesByUserIds(userIds));
    }


    @ApiOperation(value = "获取用户对应手机号", response = ResultBody.class)
    @PostMapping(value = "/getUserPhones")
    public ResultBody<List<String>> getUserPhones(@RequestBody List<String> userIds) {
        return ResultBody.success(userService.getUserPhones(userIds));
    }

    @ApiOperation(value = "根据用户ids获取用户信息", response = ResultBody.class)
    @PostMapping(value = "/getUserByUserIds")
    public ResultBody<List<UserEntity>> getUserByUserIds(@RequestBody List<String> userIds) {
        return ResultBody.success(userService.getUserByUserIds(userIds));
    }


    @ApiOperation(value = "获取用户所有角色", response = ResultBody.class)
    @GetMapping(value = "/getUserRoles/{userId}")
    private ResultBody<List<String>> getUserRoles(@PathVariable("userId") String userId) {
        return userService.getUserRoles(userId);
    }

    @ApiOperation(value = "检查用户是否存在", response = ResultBody.class)
    @GetMapping(value = "/checkUser/{userId}")
    public boolean checkUser(@PathVariable("userId") String userId) {
        return userService.checkUser(userId);
    }

    @ApiOperation(value = "通过客户端ID验证用户是否需要登录", response = ResultBody.class)
    @GetMapping(value = "/checkUserByClientId/{clientId}")
    public ResultBody checkUserByClientId(@PathVariable("clientId") String clientId) {
        return userService.checkUserByClientId(clientId);
    }


    @ApiOperation(value = "根据部门ids获取用户信息", response = ResultBody.class)
    @PostMapping(value = "/getUserByDeptIds")
    public ResultBody<List<UserEntity>> getUserByDeptIds(@RequestBody List<String> deptIds) {
        return ResultBody.success(userService.getUserByDeptIds(deptIds));
    }

    @ApiOperation(value = "根据部门id获取用户信息", response = ResultBody.class)
    @GetMapping(value = "/getUserByDeptId/{deptId}")
    public ResultBody<List<UserEntity>> getUserByDeptId(@PathVariable("deptId") String deptId) {
        return ResultBody.success(userService.getUserByDeptId(deptId));
    }

    @ApiOperation(value = "生成数据库文件", response = ResultBody.class)
    @PostMapping(value = "/exportDatabase")
    public ResultBody exportDatabase(HttpServletRequest request,HttpServletResponse response) {
        try {
            screwTemplate.createDatabaseFile(request,response);
        } catch (IOException e) {
            System.out.println("生成数据库文件失败:"+e.getMessage());
        }
        return ResultBody.success();
    }

}
